//
//  MYStatisticsViewController.m
//  投融保
//
//  Created by 523 on 13-12-23.
//  Copyright (c) 2013年 木叶科技. All rights reserved.
//

#import "MYStatisticsViewController.h"
#import "MYYongjinCell.h"
#import "MYWebService.h"
#import "ParameterYongjinDetail.h"
#import "ShouyiXQ.h"

@interface MYStatisticsViewController ()

@end

@implementation MYStatisticsViewController
@synthesize sectionHeaderView;

@synthesize arrClient;
@synthesize service;

extern NSString *key;
extern NSString *appKey;
ParameterYongjinDetail *parYongjinDetail;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.............
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable) name:@"getAsyData" object:Nil];
    
    service=[MYWebService new];
    
    [self initView];
    
    //异步取数据
    NSString *strAppBrokerageRecordService=@"service=appBrokerageRecordService&pageNo=1&pageSize=10";
    [service getDataAsynWithKey:key encryptedString:strAppBrokerageRecordService appKey:appKey];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView
{
    
    self.clientTbale.backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg"]];
    [self.clientTbale setFrame:CGRectMake(0, 368, 768, SCREEN_HEIGHT-380)];
    
}

//点击分组展开按钮事件
- (IBAction)btnSectionAction:(UIButton *)sender {
    if (sender.selected) {
        NSLog(@"用户想关闭分组");
        sender.selected=!sender.selected;
        return;
    }else
    {
        NSLog(@"用户想展开分组");
        sender.selected=!sender.selected;
        return;
    }
    
}

- (IBAction)btnReturnAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark ++++++++++++++uitableView Delegate++++++++++++++++++++
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return self.arrClient.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier=@"reuseIdentifier";
    MYYongjinCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil) {
        NSArray *arrcell=[[NSBundle mainBundle] loadNibNamed:@"MYYongjinCell" owner:self options:nil];
        cell=[arrcell objectAtIndex:0];
    }
    //    [cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yongjin_cell_bg" ]]];
    cell.lblUserName.text=[[self.arrClient objectAtIndex:indexPath.row] valueForKey:@"investorName"];
    cell.lblInvest.text=[NSString stringWithFormat:@"￥ %@",[[self.arrClient objectAtIndex:indexPath.row] valueForKey:@"investAmount"]];
    
    cell.lblEarnings.text=[NSString stringWithFormat:@"￥ %@",[[self.arrClient objectAtIndex:indexPath.row] valueForKey:@"brokerBenifitAmount"]];
    if (IOS_VERSION<7) {
        cell.selectionStyle=UITableViewCellSelectionStyleGray;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    parYongjinDetail=[ParameterYongjinDetail new];
    parYongjinDetail.tradeId=[[self.arrClient objectAtIndex:indexPath.row] valueForKey:@"tradeId"];
    parYongjinDetail.detailId=[[self.arrClient objectAtIndex:indexPath.row] valueForKey:@"detailId"];
    parYongjinDetail.investDetailId=[[self.arrClient objectAtIndex:indexPath.row] valueForKey:@"investDetailId"];
    [self presentViewController:[ShouyiXQ new] animated:YES completion:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *secitonHead = [self setSectionHeaderView:section andTableVie:tableView];
//
//    return secitonHead;
//}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 88;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 46;
//}

// 创建section 表头 的view
-(UIView *)setSectionHeaderView:(NSInteger)section andTableVie:(UITableView *)tableview{
    
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 46)];
    UILabel *monthLable =[[UILabel alloc]initWithFrame:CGRectMake(51, 9, 19, 19)];
    monthLable.text = @"月";
    monthLable.backgroundColor = [UIColor clearColor];
    monthLable.font = [UIFont systemFontOfSize:15];
    monthLable.textColor = UIColorFromRGB(0x0569a2);
    
    UIImageView *sectionBackImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 46)];
    sectionBackImage.image = [UIImage imageNamed:@"fengzu_bg"];
    UIImageView *monthImage = [[UIImageView alloc]initWithFrame:CGRectMake(8, 4, 35, 24)];
    monthImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"num_%d",section+1]];
    
    UILabel *timeLale = [[UILabel alloc]initWithFrame:CGRectMake(8, 29, 79, 16)];
    timeLale.font = [UIFont systemFontOfSize:12];
    timeLale.backgroundColor = [UIColor clearColor];
    timeLale.textColor = UIColorFromRGB(0x0569a2);
    
    UILabel *touZiLable = [[UILabel alloc]initWithFrame:CGRectMake(144, 4, 85, 21)];
    touZiLable.font = [UIFont systemFontOfSize:13];
    touZiLable.backgroundColor = [UIColor clearColor];
    touZiLable.textColor = UIColorFromRGB(0x0569a2);
    
    UILabel *touZiText = [[UILabel alloc]initWithFrame:CGRectMake(167, 23, 38, 21)];
    touZiText.text = @"投资";
    touZiText.textColor = UIColorFromRGB(0x0569a2);
    touZiText.font = [UIFont systemFontOfSize:16];
    touZiText.backgroundColor = [UIColor clearColor];
    
    UILabel *shouYiText = [[UILabel alloc]initWithFrame:CGRectMake(258, 23, 41, 21)];
    shouYiText.text = @"收益";
    shouYiText.font = [UIFont systemFontOfSize:16];
    shouYiText.backgroundColor = [UIColor clearColor];
    shouYiText.textColor = UIColorFromRGB(0x0569a2);
    
    UILabel *shouYiLable =[[UILabel alloc]initWithFrame:CGRectMake(237, 4, 83, 21)];
    shouYiLable.font = [UIFont systemFontOfSize:13];
    shouYiLable.backgroundColor = [UIColor clearColor];
    shouYiLable.textColor = UIColorFromRGB(0x0569a2);
    //添加buton
    UIButton *sectionBtn  = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 320, 46)];
    sectionBtn.backgroundColor = [UIColor clearColor];
    sectionBtn.tag = 1+section;
    [sectionBtn addTarget:self action:@selector(secitonClick:) forControlEvents:UIControlEventTouchUpInside];
    sectionBtn.selected=YES;
    //给投资收益的钱的数量赋值 ￥
    switch (tableview.tag) {
        case 10:
            sectionBtn.tag = 1+section;
            [sectionBtn addTarget:self action:@selector(secitonClick:) forControlEvents:UIControlEventTouchUpInside];
            //            touZiText.text = @"投资";
            //            shouYiText.text = @"收益";
            touZiLable.text = @"￥1000";
            shouYiLable.text =@"￥10000";
            break;
        case 11:
            sectionBtn.tag = 21+section;
            shouYiText.text = @"总划入";
            shouYiLable.text =@"￥20000";
            break;
        case 12:
            sectionBtn.tag= 41+section;
            shouYiText.text = @"总划出";
            shouYiLable.text =@"￥30000";
            break;
            
            
        default:
            break;
    }
    
    touZiLable.text = @"￥1000";
    shouYiLable.text =@"￥10000";
    timeLale.text = @"12.01-12.05";
    shouYiLable.textAlignment  = NSTextAlignmentCenter;
    touZiLable.textAlignment = NSTextAlignmentCenter;
    
    [headView addSubview:sectionBackImage];
    [headView addSubview:monthImage];
    [headView addSubview:touZiText];
    [headView addSubview:shouYiText];
    [headView addSubview:shouYiLable];
    [headView addSubview:touZiLable];
    [headView addSubview:timeLale];
    [headView addSubview:sectionBtn];
    [headView addSubview:monthLable];
    headView.userInteractionEnabled = YES;
    return headView;
}

//section点击事件 tag 1-12
-(void)secitonClick:(UIButton *)sender{
    
    if (sender.selected) {
        NSLog(@"用户想关闭分组");
        sender.selected=!sender.selected;
        return;
    }else
    {
        NSLog(@"用户想展开分组");
        sender.selected=!sender.selected;
        return;
    }
}

//收到通知刷新表
- (void)reloadTable
{
    //    [[[service.profileDataAsyn valueForKey:@"result"] valueForKey:@"dataMap"] valueForKey:@"page"];
    
    self.arrClient =[NSMutableArray arrayWithArray:[[service.profileDataAsyn valueForKey:@"result"] valueForKey:@"infos"]];
    NSLog(@"+++++++++++++++%@",service.profileDataAsyn);
    //总收益
    self.lblTotalEarnings.text=[NSString stringWithFormat:@"￥ %@",[[service.profileDataAsyn valueForKey:@"result"]  valueForKey:@"payedAmount"]];
    
    self.lblLeijiyongjin.text=[NSString stringWithFormat:@"￥ %@",[[service.profileDataAsyn valueForKey:@"result"] valueForKey:@"allAmount"]];
    
    [self.clientTbale reloadData];
    //    NSLog(@"本页面收到的数据：%@",self.arrClient);
}

#pragma mark-   -----------状态栏底色------
- (UIStatusBarStyle)preferredStatusBarStyle
{
    [self.view setBackgroundColor:[UIColor blackColor]];
    return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden
{
    return NO;
}
@end
